home *** CD-ROM | disk | FTP | other *** search
- /* #[info: */
- /************************************************************************
- * *
- * Jan van der Steen *
- * *
- * Centre for Mathematics and Computer Science *
- * Amsterdam, the Netherlands *
- * *
- *-----------------------------------------------------------------------*
- * File : options.c *
- * Purpose : Get the command line options *
- * Version : 1.10 *
- * Modified: 3/10/93 13:36:40 *
- * Author : Jan van der Steen (jansteen@cwi.nl) *
- *-----------------------------------------------------------------------*
- ************************************************************************/
- /* #]info: */
- /* #[include: */
-
- #include <stdio.h>
- #ifdef __STDC__
- # include <stdlib.h>
- #endif
-
- #include <string.h>
- #include "game2eps.h"
- #include "game2tex.h"
- #include "tools.h"
- #include "patch.h"
-
- /* #]include: */
- /* #[defines: */
-
- #define ARGCH (int)':'
- #define BADCH (int)'?'
- #define EMSG ""
- #define tell(s) fputs(*nargv,stderr); \
- fputs(s,stderr); \
- fputc(optopt,stderr); \
- fputc('\n',stderr); \
- return(BADCH);
-
- /* #]defines: */
- /* #[static: */
-
- static int optopt; /* character checked for validity */
- static char * optarg; /* option counter */
-
- /* #]static: */
- /* #[extern: */
-
- extern EPSFINFO epsf; /* The eps filename/directory */
- extern char * program; /* Program name */
- extern int prn_dev; /* DEVICE_{EPS,TEX} */
- extern int verbose; /* Verbose mode */
- extern int caching; /* Caching mode */
- extern int textout; /* Emit the text too */
- extern int statout; /* Emit the kibitz stats */
- extern int coords; /* Print coordinates */
- extern int optind; /* index into argv vector */
- extern char * optarg; /* option pointer */
- extern int figlist[]; /* Figure list */
- extern int dialist[]; /* Diagram list */
- extern int user_figs; /* Users fig list? */
- extern int user_dias; /* Users dia list? */
-
- /* #]global: */
-
- /* #]extern: */
- /* #[prototype: */
-
- #ifdef __STDC__
- # define PROTO(s) s
- #else
- # define PROTO(s) ()
- #endif
-
- static void usage PROTO((int exitcode));
- static int getopt PROTO((int nargc, char **nargv, char *ostr));
- static void getlist PROTO((char *userlist, int mylist[]));
-
- #undef PROTO
-
- /* #]prototype: */
-
- /* #[getopt: */
-
- static int
- getopt(nargc,nargv,ostr)
- int nargc;
- char ** nargv;
- char * ostr;
- {
- static char *place = EMSG; /* option letter processing */
- char *oli; /* option letter list index */
-
- if (*place == 0) { /* update scanning pointer */
- if (optind >= nargc ||
- *(place = nargv[optind]) != '-' ||
- ! *++place) return(EOF);
- if (*place == '-') { /* found "--" */
- ++optind;
- return(EOF);
- }
- } /* option letter okay? */
- if ((optopt = (int)*place++) == ARGCH ||
- (oli = strchr(ostr, optopt)) == 0) {
- if (*place == 0) ++optind;
- tell(": illegal option -- ");
- }
- if (*++oli != ARGCH) { /* don't need argument */
- optarg = NULL;
- if (*place == 0) ++optind;
- }
- else { /* need an argument */
- if (*place) optarg = place; /* no white space */
- else if (nargc <= ++optind) { /* no arg */
- place = EMSG;
- tell(": option requires an argument -- ");
- }
- else optarg = nargv[optind]; /* white space */
- place = EMSG;
- ++optind;
- }
- return(optopt); /* dump back option letter */
- }
-
- /* #]getopt: */
- /* #[getoptions: */
-
- #define OPTIONS "D:f:S:PTCchkqstv"
-
- void
- getoptions(argc, argv)
- int argc;
- char **argv;
- {
- int c;
- char *s;
- #ifndef __STDC__
- extern char *getenv();
- #endif
-
- #if __unix || unix || UNIX
- epsf.eps_dir = "/tmp";
- #else
- epsf.eps_dir = "k:\\tmp";
- #endif
- epsf.eps_size = 180;
- if ((s = getenv("EPSFTMP" )) != (char *) 0) epsf.eps_dir = s;
- if ((s = getenv("EPSFSIZE")) != (char *) 0) epsf.eps_size = atoi(s);
-
- while ((c = getopt(argc, argv, OPTIONS)) != EOF)
- switch (c) {
- /* EPS directory */
- case 'D': epsf.eps_dir = optarg; break;
-
- /* Emit EPS diagrams */
- case 'P': prn_dev = DEVICE_EPS; break;
-
- /* Emit TeX diagrams */
- case 'T': prn_dev = DEVICE_TEX; break;
-
- /* Enable caching */
- case 'C': caching = 1; break;
-
- /* Set diagram size */
- case 'S': epsf.eps_size = atoi(optarg); break;
-
- case 'f': /* Get user list of figures */
- getlist(optarg, figlist);
- user_figs = 1; break;
-
- case 'd': /* Get user list of diagrams */
- getlist(optarg, dialist);
- user_dias = 1; break;
-
- /* Disable coordinates */
- case 'c': coords = 0; break;
-
- /* Disable statistics output */
- case 's': statout = 1; break;
-
- /* Dispose text */
- case 't': textout = 0; break;
-
- /* Set message level */
- case 'v': verbose = 1; break;
-
- /* Show program version and exit(0) */
- case 'q': fprintf(stdout, "%s-%d.%d\n", program,
- RELEASE, PATCHLEVEL);
- exit(0);
-
- case 'h': usage(0); /* Online help */
- case '?':
- default : usage(1); /* Error */
- }
- }
-
- /* #]getoptions: */
- /* #[usage: */
-
- #define FMTS2(s1,s2) fprintf(stderr, "%-16s%-40s\n", s1, s2)
- #define FMTS3(s1,s2,s3) fprintf(stderr, "%-16s%-40s%s\n", s1, s2, s3)
- #define FMTN3(s1,s2,n1) fprintf(stderr, "%-16s%-40s%d\n", s1, s2, n1)
- #define DOTTEDLINE {\
- int i;\
- for (i = 0; i < 70; i++) fputc('-', stderr);\
- fputc('\n', stderr);\
- }
-
- static void
- usage(code)
- int code;
- {
- fprintf(stderr , "Usage: %s [options] [file]\n", program);
- DOTTEDLINE
- FMTS3("Option" , "Description" , "Default");
- DOTTEDLINE
- FMTS3("[-D path]" , "EPS directory" , epsf.eps_dir);
- FMTS3("[-P]" , "Emit PS for diagrams" , "on");
- FMTS3("[-T]" , "Emit TeX for diagrams" , "off");
- FMTS3("[-C]" , "Caching mode" , "off");
- FMTN3("[-S size]" , "Diagram size [72 = 1in]" , epsf.eps_size);
- FMTS3("[-d list]" , "Emit diagrams in list" , "off");
- FMTS3("[-f list]" , "Emit figures in list" , "when text");
- FMTS3("[-s]" , "Emit kibitz statistics" , "off");
- FMTS3("[-c]" , "Don't print coordinates" , "on");
- FMTS3("[-t]" , "Don't print text output" , "on");
- FMTS3("[-v]" , "Verbose mode" , "off");
- FMTS2("[-q]" , "Print program version");
- FMTS2("[-h]" , "Print usage");
-
- exit(code);
- }
-
- /* #]usage: */
-
- /* #[getlist: */
-
- static void
- getlist(userlist, mylist)
- /*
- * Get the list of figures to print
- *
- * List can be any of:
- *
- * %n Create figure after each n moves
- * k+l+...+n Create figure at move k, l, ..., n
- *
- * We fill the mylist array with the move/diagram numbers where we
- * will print a figure/diagram
- */
- char *userlist;
- int mylist[];
- {
- int interval;
- int number;
- int n = 0;
-
- /*
- * First we fill the array with numbers != 0
- */
- for (n = 0; n < MAXNUM; n++) figlist[n] = 999; n = 0;
- if (*userlist == '%') {
- if (mylist == dialist) {
- fprintf(stderr,
- "%s: This format is only allowed for figures\n",
- program);
- exit(1);
- }
- /*
- * Interval request
- */
- interval = atoi(userlist+1);
- if (interval < 1 || interval >= MAXNUM) {
- fprintf(stderr,
- "%s: Figure interval out of range (1 - %d)\n",
- program, MAXNUM);
- exit(1);
- }
- while ((number = (n+1)*interval) < MAXNUM) mylist[n++] = number;
- }
- else {
- /*
- * User list request
- */
- char *delim = "+,-", *t;
-
- t = strtok(userlist, delim);
- while (t != NULL) {
- number = atoi(t);
- if (number >= MAXNUM) {
- if (mylist == figlist)
- fprintf(stderr,
- "%s: Move number %d out of range (< %d)\n",
- program, number, MAXNUM);
- else
- fprintf(stderr,
- "%s: Diagram number %d out of range (< %d)\n",
- program, number, MAXNUM);
- exit(1);
- }
- mylist[n++] = number;
- t = strtok(NULL, delim);
- }
- }
- }
-
- /* #]getlist: */
-
-